package com.fu.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author 付大大
 * @description   ServletContext对象的创建和销毁的监听器
 * @create 2021-01-26 11:25
 */
@WebListener
public class ServletContextListenerDemo implements ServletContextListener {

    /**
     * ServletContext对象创建的时候执行此方法
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听到了对象的创建");

        ServletContext servletContext = sce.getServletContext();

        servletContext.setAttribute("username","zhangsan");

        servletContext.setAttribute("username", "lisi");

        servletContext.removeAttribute("username");
    }

    /**
     * 销毁的时候执行
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("监听到了对象的销毁");
    }
}
